#include "hw_types.h"
#include "osram96x16x1.h"

#include "coefficients.h"

#include <string.h>
#include <ctype.h>

extern unsigned char state;
extern unsigned int seconds;

int validCoefficient(char str[])
{
	int currPos, oldCurrPos;
	
	//the string should start with a digit or minus sign
	if (!(str[0] == '-' || isdigit(str[0])))
	{
		str[0] = '\0';
		return 0;
	}
	
	//determines wheather the 1st digit is on 1st or 2nd position
	if (isdigit(str[0]))
		currPos = 0;
	else
		currPos = 1;
	
	//saves the position of the 1st digit
	oldCurrPos = currPos;
	
	//checks if we have inputed . (dot)
	if (strstr(str, ".") != NULL)
	{
		//scans all the symbols between the 1st digit and the . (dot)
		while (str[currPos] != '.')
		{
			//if it is not a digit, abort program
			if(!isdigit(str[currPos]))
			{	
				str[0] = '\0';
				return 0;
			}
			currPos++;
		}
		
		//checks wheater there are more than 3 digits entered OR
		//if there are no symbols between the '-' and '.'
		if ((currPos - oldCurrPos) > 3 || (currPos - oldCurrPos == 0))
		{	
			str[0] = '\0';
			return 0;
		}

		//move currPos to be positioned at the 1st digit after the '.'
		currPos++;
		oldCurrPos = currPos;
	}
	
	//scans all the symbols till the end of the string
	while (currPos != strlen(str))
	{
		//if it is not a digit, abort program
		if(!isdigit(str[currPos]))
		{
			str[0] = '\0';
			return 0;
		}
		currPos++;
	}
	
	//checks wheater there are more than 3 digits entered
	if ((currPos - oldCurrPos) > 3 || (currPos - oldCurrPos) == 0)
	{
		str[0] = '\0';
		return 0;
	}
	
	return 1;
}

//
// Parameter a range from 0 till 100%, depending on the potentiometer
//
void inputCoefficients(char str[])
{
	//position where we write in the string
	int index = 0;		
	//percentage of the full capacity of the potentiometer
	int result = 0, oldResult;
	
	unsigned long i;

	while (state != 2)
	{
	
	   if(screenStart())
	   		return;
		
		oldResult = result;
		result = GetDataFromACP();
		if (result != oldResult)
			seconds = 0;
		 	
		OSRAM96x16x1StringDraw("Form:(-)XXX.XXX", 0, 1);

		if (result < 7)
			OSRAM96x16x1StringDraw("0", index*6, 0);
		else if (result < 15)
			OSRAM96x16x1StringDraw("1", index*6, 0);
		else if (result < 23)	
			OSRAM96x16x1StringDraw("2", index*6, 0);
		else if (result < 31)
			OSRAM96x16x1StringDraw("3", index*6, 0);
		else if (result < 40)
			OSRAM96x16x1StringDraw("4", index*6, 0);
		else if (result < 48)		
			OSRAM96x16x1StringDraw("5", index*6, 0);
		else if (result < 56)
			OSRAM96x16x1StringDraw("6", index*6, 0);
		else if (result < 66)				
			OSRAM96x16x1StringDraw("7", index*6, 0);
		else if (result < 76)
			OSRAM96x16x1StringDraw("8", index*6, 0);
		else if (result < 86)
			OSRAM96x16x1StringDraw("9", index*6, 0);
		else if (result < 95)				
			OSRAM96x16x1StringDraw("-", index*6, 0);
		else 
			OSRAM96x16x1StringDraw(".", index*6, 0);
		 
		if (state == 1)
		{ 	
			state = 0;
			if (result < 7)
				str[index] = '0';
			else if (result < 15)
				str[index] = '1';
			else if (result < 23)
				str[index] = '2';
			else if (result < 31)
				str[index] = '3';
			else if (result < 40)
				str[index] = '4';
			else if (result < 48)
				str[index] = '5';
			else if (result < 56)
				str[index] = '6';
			else if (result < 66)
				str[index] = '7';
			else if (result < 76)
				str[index] = '8';
			else if (result < 86)
				str[index] = '9';
			else if (result < 95)
				str[index] = '-';
			else 
				str[index] = '.';
			// Write to next position next time
			index++;
		}
		
		//marks the end of the string
		str[index] = '\0';

		// Just before the timer expires, check out if the entered string is valid
		if (seconds == 9)
		{
			if (!validCoefficient(str))
				str[0] = '\0';
		}
	}

	//if nothing entered, return to main menu
	if (strlen(str) == 0)
	{
		OSRAM96x16x1Clear();	
		menuDraw();
		return;
	}
	//
	//Checks if the coefficient is with the valid form
	//
	if (!validCoefficient(str))
	{
		state = 0;
		OSRAM96x16x1Clear();	
		OSRAM96x16x1StringDraw("INVALID", 0, 0);
		OSRAM96x16x1StringDraw("COEFFICIENT", 0, 1);
		for (i=2500000; i>0; i--)
			;
		OSRAM96x16x1Clear();
		inputCoefficients(str);
	}

	OSRAM96x16x1Clear();
	menuDraw();
}
